<template>
  <div>
    <!-- 搜索 -->
    <!-- 预授权申请 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :is-select-column="false"
      columnsRef="AuthApplicationlgr"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</multiuse-button>
        <!-- <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="exportFileBtn()">导出</multiuse-button> -->
      </template>
    </FilterTable>
    <Drawer :visible.sync="visibleAdd" :title="'预授权新增'" destroyOnClose width="100%" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <addForm @close="closeAdd" @save="saveAdd" type></addForm>
    </Drawer>
    <BaseDialog :visible.sync="visible" :title="'车型新增'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <vehicle @close="close" @save="save"></vehicle>
    </BaseDialog>
    <Drawer :visible.sync="visibleEdit" :title="'预授权编辑'" width="100%" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <editForm @close="editclose" @save="editsave" :rowData="rowData"></editForm>
    </Drawer>
    <Drawer :visible.sync="visibleDetail" :title="'预授权详情'" width="100%"  destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <detailForm @close="detailclose" @save="editsave" :rowData="rowData"></detailForm>
    </Drawer>
  </div>
</template>
<script>
import moment from "moment";
  import vehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
  import addForm from './addForm.vue';
  import editForm from './editForm.vue';
  import detailForm from './detailForm.vue';
  import { getAppId } from '@/assets/js/auth';
  import { getListByPage,deleteOne } from '@/api/claim/dealer/AuthApplication';
  import { dictionary } from '@/utils/dictMixin';
  export default {
    mixins: [dictionary],
    components: {
      vehicle,
      addForm,
      editForm,
      detailForm
    },
    data() {
      return {
        visible: false,
        visibleDrawer: false,
        collapse: true,
        visibleEdit: false,
        visibleDetail:false,
        infoId: null,
        rowData: {},
        fetchapi: getListByPage,
        topFilterInit: this.topFilterInitFun(),
        flterValue: {}, //参数
        columns: this.creatHeader(),
        fetchParams: {querFlag:1,xhrAbort: true}, //ipa 接口
        selectedRows: [], //选中经销商
        dialogTitle: '选择车辆',
        visibleAdd: false,
        authNo: '' //预授权单号
      };
    },
    created() {
      // if (getAppId() == 'arcfox') {
      //   this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList =this.createDictList('5543',[55431001])
      // }else{
      //   this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList =this.createDictList('5543')
      // }
      this.chuqi();
    },
    methods: {
      //搜索数据
      topFilterInitFun() {
        return [
          {type: 'INPUT', label: '预授权单号', fieldName: 'authNo', placeholder: '请输入预授权单号...', initialValue: '' },
          // { type: 'INPUT', label: '维修工单号', fieldName: 'roNo', placeholder: '请输入', initialValue: '' },
          {type: 'SELECT', label: '预授权状态', fieldName: 'authStatus', placeholder: '请选择预授权状态...', initialValue: '',
            itemList:this.createDictList('5503',[55031005,55031006]), filterable: true},
          {type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入VIN...', initialValue: '' },
          {type: 'INPUT', label: '车牌号', fieldName: 'licenseNo', placeholder: '请输入车牌号...', initialValue: '' },
          {type: 'RANGE_DATE', label: '申请时间', fieldName: 'beginApplyAt|endApplyAt', placeholder: '请输入',initialValue: [
          moment()
            .startOf('week')
            .format('YYYY-MM-DD HH:mm:ss'),
          moment()
            .endOf('week')
            .format('YYYY-MM-DD HH:mm:ss')
        ] },
          // {type: 'INPUT', label: '发动机号/电机号', fieldName: 'engineNo', placeholder: '请输入发动机号/电机号...', initialValue: '' },
          {type: 'INPUT', label: '主损件名称', fieldName: 'partName', placeholder: '请输入主损件名称...', initialValue: '' },
          {type: 'INPUT', label: '主损件代码', fieldName: 'partCode', placeholder: '请输入主损件代码...', initialValue: '' },
          {type: 'SELECT', label: '预授权类型', fieldName: 'authType', placeholder: '请选择预授权类型...', initialValue: '', itemList:this.createDictList('5508',[]), filterable: true}
        ];
      },
      //搜索获取数据
      changeHandle(val) {
        // if(!val.beginCreatedAt) {
        //   this.$notify.warning({ title: '提示信息', message: '创建开始日期不可为空！' });
        //   return
        // }
        // if(!val.endCreatedAt) {
        //   this.$notify.warning({ title: '提示信息', message: '创建结束日期不可为空！' });
        //   return
        // }
        // if(getAppId() == 'arcfox') {
        //   if (moment(moment(val.endCreatedAt).format('YYYY-MM-DD')).diff(moment(val.beginCreatedAt), 'months', true) > 12) {
        //     this.$notify.warning({ title: '提示信息', message: '创建日期跨度不可超过十二个月！' });
        //     return
        //   }
        // } else {
        //   if (moment(moment(val.endCreatedAt).format('YYYY-MM-DD')).diff(moment(val.beginCreatedAt), 'months', true) > 12) {
        //     this.$notify.warning({ title: '提示信息', message: '创建日期跨度不可超过十二个月！' });
        //     return
        //   }
        // }
        this.fetchParams = Object.assign({querFlag:1}, this.fetchParams, val,{xhrAbort: false});
        // this.SELECT();
      },
      //table表头和数据源
      creatHeader() {
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 80,
            sorter: true,
            fixed: 'left',
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          {
            title: '操作',
            dataIndex: 'column-action',
            width: 100,
            render: props => {
              return (
                <div style="text-align:center">
                <multiuse-button size="mini" type="text" disabled={((props.row.authStatus == '55031002') || (props.row.authStatus =='55031003')|| (props.row.authStatus =='55031005')|| (props.row.authStatus =='55031006')  )? true: false } onClick={() => this.edit(props.row)} >
                  编辑
                </multiuse-button>
                <multiuse-button size="mini" type="text" onClick={() => this.detail(props.row)}>
                      明细
                </multiuse-button>

                </div>
            );
            }
          },
          // <multiuse-button size="mini" type="text" disabled={((props.row.authStatus == '55031002') || (props.row.authStatus =='55031003')||(props.row.authStatus =='55031005')|| (props.row.authStatus =='55031006')  )? true: false } onClick={() => this.delFun(props.row)}>
          //   作废
          // </multiuse-button>
          { title: '预授权单号', dataIndex: 'authNo', sorter: true },
          { title: '预授权状态', dataIndex: 'statusStr', sorter: true },
          { title: '维修工单号', dataIndex: 'roNo', sorter: true },
          { title: 'VIN', dataIndex: 'vin', sorter: true },
          { title: '车牌号', dataIndex: 'licenseNo', sorter: true },
          { title: '车型名称', dataIndex: 'modelName', sorter: true },
          { title: '预授权类型', dataIndex: 'authTypeStr', sorter: true },
          { title: '常规预授权分类', dataIndex: 'authSubStr', sorter: true },
          { title: '主损件代码', dataIndex: 'partCode', sorter: true },
          { title: '主损件名称', dataIndex: 'partName', sorter: true },
          { title: '供应商', dataIndex: 'supName', sorter: true },
          { title: '行驶里程', dataIndex: 'driveMile', sorter: true },
          // { title: '发动机号/电机号', dataIndex: 'engineNo', sorter: true },
          { title: '故障现象代码', dataIndex: 'faultCode', sorter: true },
          { title: '故障现象名称', dataIndex: 'faultName', sorter: true },
          { title: '故障原因代码', dataIndex: 'frCode', sorter: true },
          { title: '故障原因名称', dataIndex: 'frName', sorter: true },
          // { title: '驳回次数', dataIndex: 'channelNum', sorter: true },
          { title: '费用合计', dataIndex: 'sumAmount', sorter: true },
          { title: '工时费', dataIndex: 'labourAmount', sorter: true },
          { title: '备件费', dataIndex: 'partAmount', sorter: true },
          { title: '附加费用', dataIndex: 'addAmount', sorter: true },
          { title: '保修起始日期', dataIndex: 'repairBeginDate', sorter: true , dateFormat: 'yyyy-MM-dd'},
          { title: '保修截止日期', dataIndex: 'repairEndDate', sorter: true , dateFormat: 'yyyy-MM-dd'},
         // { title: '生产日期', dataIndex: 'prodDate', sorter: true , dateFormat: 'yyyy-MM-dd'},
          { title: '销售日期', dataIndex: 'saleDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
         { title: '开单日期', dataIndex: 'serviceBeginDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
          { title: '申请人', dataIndex: 'applyBy', sorter: true },
          { title: '申请时间', dataIndex: 'applyAt', sorter: true, width: 140 },
          { title: '审核人', dataIndex: 'checkBy', sorter: true },
          { title: '审核时间', dataIndex: 'checkAt', sorter: true },
          // { title: '主键', dataIndex: 'authId', sorter: true, hidden: true }
        ];
      },
      //选中
      selectChange(rows) {
        this.selectedRows = rows;
        console.log(this.selectedRows);
      },
      async chuqi() {
        // const res = await getPartType({ type: 5503 });
        // delete res.data[1];
        // delete res.data[2];
        // delete res.data[4];
        // delete res.data[5];
        // if (res.resultCode == 200) {
        //   this.topFilterInit.find(x => x.fieldName == 'authStatus').initialValue = '';
        //   this.topFilterInit.find(x => x.fieldName == 'authStatus').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
        // }
        // const res4 = await getPartType( {type:5543});
        // if (res4.resultCode == 200) {
        //
        //   if (getAppId() == 'arcfox') {
        //     delete res4.data[0];
        //   }
        //
        //   this.topFilterInit.find(x => x.fieldName == 'ifComm').initialValue = '';
        //   this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList = res4.data.map(y =>({ text: y.codeCnDesc, value: y.codeId }));
        // }

      },
      // async claimType() {

        // const res = await getclaimType({ type: 5501 });
        // if (res.resultCode == 200) {
        //   this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').initialValue = '';
        //   this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
        // }
        // const res2 = await getClaim();
        // if (res2.resultCode == 200) {
        //   delete res2.data[7];
        //   this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').initialValue = '';
        //   this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').itemList = res2.data.map(y => ({ text: y.name, value: y.code }));
        // }
      // },
// 删除
      async delFun(data) {
        this.$confirm('确定删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            this.res = await deleteOne(data.authId);
            if (this.res.resultCode != 200) {
              this.$notify.error({ title: '提示信息', message: res.errMsg || '删除失败!' });
            } else {
              this.$notify.success({ title: '提示信息', message: '删除成功!' });
              // 重新加载
              this.fetchParams = { ...this.fetchParams };
            }
          })

        // let res = [];
        // res = await deleteOne({ ptId: data.ptId });
        // if (res.resultCode == 200) {
        //   this.fetchParams = Object.assign({}, this.fetchParams);
        // } else if (res.resultCode == '403') {
        //   console.info('不能删除:{}', res);
        //   this.$notify.error({
        //     title: '失败',
        //     message: res.errMsg
        //   });
        // }
      },

      close() {
        this.visible = false;
      },
      chooseOrderNo() {
        this.visible = true;
      },
      save(obj) {
        this.topFilterInit.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
        this.visible = false;
      },
      add() {
        this.visibleAdd = true;
      },
      closeAdd() {
        this.visibleAdd = false;
      },
      saveAdd() {
        this.visibleAdd = false;
        this.fetchParams = { ...this.fetchParams };
      },
      editclose() {
        this.visibleEdit = false;
      },
      edit(val) {
        this.rowData = val;
        this.visibleEdit = true;
      },
      editsave() {
        this.visibleEdit = false;
        this.fetchParams = { ...this.fetchParams };
      },
      detailclose() {
        this.visibleDetail = false;
      },
      detail(val) {
        this.rowData = val;
        this.visibleDetail = true;
      },
    }
    // mounted() {
    //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
    // }
  };
</script>

<style lang="less" scoped></style>
